// subcontract/Mine/mineIncome/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aliyunServrBaseURL: getApp().config.aliyunServrBaseURL,//阿里云公共地址
    currentDate: new Date().getTime(),
    time:new Date().getTime(),
    datetimeShow: false,
    choiceData: getApp().timestampToDate(new Date().getTime(), 'Y年m月'),
    tabs: ['收入', '支出'],
    activeIndex: 0,//选择的下标
    moneyInfo: {},//收益信息
    page: 1,//页码
    pagingState: false,//分页状态
    moneyList: [],//明细
  },
  onChange(event) {
    let index = event.detail.index
    this.setData({
      activeIndex: index,
      page: 1,//页码
      pagingState: false,//分页状态
      moneyList: [],//明细
    })
    if (index == 0) {
      this.mymoneylist()
    } else {
      this.mymoneyapplylist()
    }
  },
  /**支出明细 */
  async mymoneyapplylist() {
    let object = {
      page_no: this.data.page,
      month: getApp().timestampToDate(this.data.currentDate, 'Y-m')
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.mymoneyapplylist(object)
      if (code == 1) {
        this.setData({
          moneyList: this.data.moneyList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /**收入明细列表 */
  async mymoneylist() {
    let object = {
      page_no: this.data.page,
      month: getApp().timestampToDate(this.data.currentDate, 'Y-m')
    }
    if (!this.data.pagingState) {
      const { code, data } = await getApp().api.mymoneylist(object)
      if (code == 1) {
        this.setData({
          moneyList: this.data.moneyList.concat(data.lists)
        })
        if (data.more != 1) {
          this.setData({
            pagingState: true
          })
          return
        }
        this.setData({
          page: (this.data.page * 1) + 1
        })
      }
    }
  },
  /**我的收入 */
  async userMymoney() {
    const { code, data } = await getApp().api.userMymoney()
    if (code == 1) {
      this.setData({
        moneyInfo: data
      })
    }
  },
  navigateTo(event) {
    let url = event.currentTarget.dataset.url
    wx.navigateTo({
      url,
    })
  },
  showPopup() {
    this.setData({ datetimeShow: true });
  },

  onClose() {
    this.setData({ datetimeShow: false });
  },
  confirmDatetime(event) {
    console.log(event);
    this.setData({
      page: 1,//页码
      pagingState: false,//分页状态
      moneyList: [],//明细
      currentDate: event.detail,
      choiceData: getApp().timestampToDate(event.detail, 'Y年m月'),
      datetimeShow: false
    });
    this.mymoneylist()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      page: 1,//页码
      pagingState: false,//分页状态
      moneyList: [],//明细
    })
    this.userMymoney()
    this.mymoneylist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})